package com.ruoyi.project.instrument.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.StringUtils;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.instrument.domain.InsReagent;
import com.ruoyi.project.instrument.service.IInsReagentService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 试剂信息Controller
 *
 * @author taney
 * @date 2020-06-10
 */
@AllArgsConstructor
@RestController
@RequestMapping("/instrument/reagent" )
public class InsReagentController extends BaseController {

    private final IInsReagentService iInsReagentService;

    /**
     * 查询试剂信息列表
     */
    @PreAuthorize("@ss.hasPermi('instrument:reagent:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsReagent insReagent)
    {
        startPage();
        LambdaQueryWrapper<InsReagent> lqw = new LambdaQueryWrapper<InsReagent>();
        if (StringUtils.isNotBlank(insReagent.getBarcode())){
            lqw.eq(InsReagent::getBarcode ,insReagent.getBarcode());
        }
        if (StringUtils.isNotBlank(insReagent.getProjectName())){
            lqw.like(InsReagent::getProjectName ,insReagent.getProjectName());
        }
        if (StringUtils.isNotBlank(insReagent.getName())){
            lqw.like(InsReagent::getName ,insReagent.getName());
        }
        if (StringUtils.isNotBlank(insReagent.getCalibrationFlag())){
            lqw.eq(InsReagent::getCalibrationFlag ,insReagent.getCalibrationFlag());
        }
        if (insReagent.getResidue() != null){
            lqw.eq(InsReagent::getResidue ,insReagent.getResidue());
        }
        if (StringUtils.isNotBlank(insReagent.getExpiredFlag())){
            lqw.eq(InsReagent::getExpiredFlag ,insReagent.getExpiredFlag());
        }
        if (StringUtils.isNotBlank(insReagent.getUseFlag())){
            lqw.eq(InsReagent::getUseFlag ,insReagent.getUseFlag());
        }
        if (StringUtils.isNotBlank(insReagent.getReagentType())){
            lqw.eq(InsReagent::getReagentType ,insReagent.getReagentType());
        }
        List<InsReagent> list = iInsReagentService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出试剂信息列表
     */
    @PreAuthorize("@ss.hasPermi('instrument:reagent:export')" )
    @Log(title = "试剂信息" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(InsReagent insReagent) {
        LambdaQueryWrapper<InsReagent> lqw = new LambdaQueryWrapper<InsReagent>(insReagent);
        List<InsReagent> list = iInsReagentService.list(lqw);
        ExcelUtil<InsReagent> util = new ExcelUtil<InsReagent>(InsReagent. class);
        return util.exportExcel(list, "reagent" );
    }

    /**
     * 获取试剂信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('instrument:reagent:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iInsReagentService.getById(id));
    }

    /**
     * 新增试剂信息
     */
    @PreAuthorize("@ss.hasPermi('instrument:reagent:add')" )
    @Log(title = "试剂信息" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InsReagent insReagent) {
        return toAjax(iInsReagentService.save(insReagent) ? 1 : 0);
    }

    /**
     * 修改试剂信息
     */
    @PreAuthorize("@ss.hasPermi('instrument:reagent:edit')" )
    @Log(title = "试剂信息" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InsReagent insReagent) {
        return toAjax(iInsReagentService.updateById(insReagent) ? 1 : 0);
    }

    /**
     * 删除试剂信息
     */
    @PreAuthorize("@ss.hasPermi('instrument:reagent:remove')" )
    @Log(title = "试剂信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iInsReagentService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
